<!--
 * @Author: only
 * @Date: 2025-03-26 13:16:15
 * @LastEditors: sueRimn
 * @LastEditTime: 2025-04-07 10:21:29
-->
<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="年份月" prop="date">
        <el-date-picker
          v-model="queryParams.date"
          type="monthrange"
          range-separator="至"
          start-placeholder="开始月份"
          end-placeholder="结束月份"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="科室" prop="deptId">
        <el-select
          v-model="queryParams.deptId"
          placeholder="请选择科室"
          clearable
        >
          <el-option
            v-for="item in deptOptions"
            :key="item.deptId"
            :label="item.deptName"
            :value="item.deptId"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          >批量导出</el-button
        >
      </el-col>
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="tableData"
      row-key="deptId"
      show-summary
      :summary-method="getSummaries"
    >
      <el-table-column prop="reportDate" label="年份月"></el-table-column>
      <el-table-column prop="totalCount" label="总数量"></el-table-column>
      <el-table-column prop="revokeCount" label="撤件数量"></el-table-column>
      <el-table-column
        prop="waitingDistributionCount"
        label="待分配数量"
      ></el-table-column>
      <el-table-column
        prop="waitingCompleteCount"
        label="待办结数量"
      ></el-table-column>
      <el-table-column
        prop="completedCount"
        label="已办结数量"
      ></el-table-column>
    </el-table>
  </div>
</template>

<script>
import moment from "moment";
import { listDept } from "@/api/system/dept";
import { staticCount } from "@/api/statistics/index";
export default {
  data() {
    return {
      // 遮罩层
      loading: false,
      // 显示搜索条件
      showSearch: true,
      // 表格树数据
      tableData: [],
      // 科室树选项
      deptOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        date: undefined,
        deptId: undefined,
      },
    };
  },
  created() {
    this.getDeptList();
    this.getLoad();
  },
  methods: {
    /** 加载列表数据 */
    getLoad() {
      this.loading = true;
      let params = { ...this.queryParams };
      if (params.date) {
        params.startDate = moment(params.date[0]).format("YYYY-MM");
        params.endDate = moment(params.date[1]).format("YYYY-MM");
        delete params.date;
      }
      staticCount(params).then((response) => {
        this.tableData = response.data || [];
        this.loading = false;
      });
    },
    /** 查询科室列表 */
    getDeptList() {
      listDept({}).then((response) => {
        let data = response.data || [];
        data = data.filter((item) => {
          return item.deptId !== 100 && item.deptId !== 203;
        });
        this.deptOptions = data;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.getLoad();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    //合并单元格
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "总计";
          return;
        }
        const values = data.map((item) => Number(item[column.property]));
        if (!values.every((value) => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
        }
      });

      return sums;
    },

    /** 导出按钮操作 */
    handleExport() {
      let params = { ...this.queryParams };
      if (params.date) {
        params.startDate = moment(params.date[0]).format("YYYY-MM");
        params.endDate = moment(params.date[1]).format("YYYY-MM");
        delete params.date;
      }
      this.download(
        "vendor/home/export_static_count",
        {
          ...params,
        },
        `统计报表_${new Date().getTime()}.xlsx`,
        {
          headers: { "Content-Type": "application/json;charset=utf-8" },
          isTansParams: true,
        }
      );
    },
  },
};
</script>
